//
//  TabbarViewController.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class TabbarViewController: UITabBarController {
    
    
//    private lazy var composeView: HMComposeView = {
//        let view = HMComposeView()
//        return view
//    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let composeButtonClickClosure = {[weak self]in
            let composeView = HMComposeView()
//           调用 HMComposeView 的show方法
            composeView.show(self)
//            把加号界面添加到tabbar上
//            self?.view.addSubview(composeView)
            
        }
        
        let tabBar = Tabbar()
        //        tabBar.composeButtonClick()
        tabBar.composeButtonClickClosure = composeButtonClickClosure
        
        // 闭包强引用控制器
        // 控制器强引用tabBar
        // tabBar里面强引用闭包
       
        
        // 只读属性，直接设置行不通
        // 如果系统的一些只读属性，私有属性，不能直接设置值的话。就可以使用 kvc 去设置
        // kvc的底层是运行时 runtime
        self.setValue(tabBar, forKey: "tabBar")
        
        
        addChildViewController(HomeViewController(), title: "主页", imgNamed: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imgNamed: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), title: "发现", imgNamed: "tabbar_discover")
        addChildViewController(MEviewController(), title: "我", imgNamed: "tabbar_profile")
        
        
    }
    
    
    
    //    添加控制器到tabbar的方法
    func addChildViewController(vc: UIViewController, title: String, imgNamed: String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imgNamed)
        //        设置image渲染模式为图片原有的样式
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgNamed)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //        设置文字点击状态颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //        设置传进来的控制器为导航控制器的主控制器
        addChildViewController(UINavigationController(rootViewController: (vc)))
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
